package com.sc.weixin.consultComment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sc.dal.common.pagination.PaginationBean;
import com.sc.dal.mapper.goods.GoodsMapper;
import com.sc.dal.model.consultComment.ConsultComment;
import com.sc.dal.model.member.Member;
import com.sc.service.consultComment.ConsultCommentService;
import com.sc.service.goods.GoodsService;
import com.sc.util.data.Common;
import com.sc.util.data.Constants;
import com.sc.util.data.DateUtil;
import com.sc.util.data.SessionContainer;

/***
 * 个人足迹信息
 * @author xiongyong
 *
 */
@Controller
@RequestMapping(value="/tracks")
public class TracksController {

	@Autowired
	private ConsultCommentService consultCommentService;
	@Autowired
	private GoodsService goodsService;
	
	@RequestMapping(value="/initTracks")
	public Object initTracks(HttpServletRequest request,Integer currPage,Integer pageSize){
		ModelAndView mav = new ModelAndView("/tracks/my_tracks");
		try {
			//判断会员是否登录
			Member member = SessionContainer.getSessionMember(request);// 根据sessionId获取登录用户
			if(member==null){
				return mav;
			}
			String mName = member.getmId();
			List<ConsultComment> consultList = null;
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("type", Constants.TYPE_C);  //3、我的足迹' 
			if(Common.isNotEmpty(mName)){
				paramMap.put("creater", mName);
			}
			/**我的足迹---商品分类信息***/
			consultList = consultCommentService.queryMessageList(paramMap);
			//封装商品分类ID、商品分类名称。
			HashSet<String>  classList =  findRemoval(consultList);
			mav.addObject("classList", classList);
			String className = request.getParameter("className");
			if(className!=null && !"".equals(className)){
				paramMap.put("className", className);
			}
			/**根据ID删除浏览信息***/
			String id = request.getParameter("id");
			//上周一
			String lastMonday = DateUtil.getLastMonday();
			long start = DateUtil.formatyMd(lastMonday).getTime();
	//		paramMap.put("lastMonday", lastMonday);
			//上周日
			String lastSunday = DateUtil.getLastSunday();
			long end = DateUtil.formatyMd(lastSunday).getTime();
	//		paramMap.put("lastSunday", lastSunday);
			List<ConsultComment> lastWeek = new ArrayList<ConsultComment>();
			List<ConsultComment> thisWeek = new ArrayList<ConsultComment>();
			List<ConsultComment> beforeWeek = new ArrayList<ConsultComment>();
			//判断ID是否为空，如不为空，则先删除足迹信息，在做查询足迹
			if(id!=null && !"".equals(id)){
				this.consultCommentService.deleteConsult(new Integer(id));
			}
			consultList = consultCommentService.queryMessageList(paramMap);
			for (ConsultComment consultComment : consultList) {
				if(consultComment.getCreateTime()!=null){
					String createTime = DateUtil.formatYMDToString(consultComment.getCreateTime());
					long createT = DateUtil.formatyMd(createTime).getTime();
					//上周的数据
					if(createT>=start && createT<=end){
						lastWeek.add(consultComment);
					}
					//本周的数据
					if(createT>end){
						thisWeek.add(consultComment);
					}
					//上周更早的数据
					if(createT<start){
						beforeWeek.add(consultComment);
					}
				}
			}
			Map<String, Object> paramHotMap = new HashMap<String, Object>();
			//搜索热卖推荐商品
			paramHotMap.put("type", "04");
			PaginationBean<Map<String, Object>> paginationHot = goodsService.getPagingDatas(paramHotMap, currPage, pageSize, "queryGoodsByClassIdCount", "queryGoodsByClassIdList", GoodsMapper.class);
			mav.addObject("goodsHotList", paginationHot.getPageList());
			mav.addObject("thisWeek", thisWeek);
			mav.addObject("lastWeek", lastWeek);
			mav.addObject("beforeWeek", beforeWeek);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	public HashSet<String> findRemoval(List<ConsultComment> consultList){
		//封装商品分类ID、商品分类名称。
		List<String> listTemp = new ArrayList<String>();
		List<String> listTemp2 = new ArrayList<String>();
		for (ConsultComment consultComment : consultList) {
			String className = consultComment.getClassName();
			listTemp.add(className);
			listTemp2.add(consultComment.getClassId());
		}
		HashSet<String> classNameSet = new HashSet<String>(listTemp);
		HashSet<String> classIdSet = new HashSet<String>(listTemp2);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("classIdSet", classIdSet);
		map.put("classNameSet", classNameSet);
		/*for (String string : hs) {
			System.out.println(string);
		}*/
		return classNameSet;
	}
	
	
	/***
	 * 
	 * 1、订单列表：猜你喜欢的商品【1、 获取曾经浏览的商品，2、如果为空，则获取热销的商品【暂未做查询】】
	 * 2、订单详情： 最近浏览过的商品
	 * @param request
	 * @param currPage
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/latelyTracks")
	@ResponseBody
	public Object latelyTracks(HttpServletRequest request){
		List<ConsultComment> consultList = null;
		try {
			//判断会员是否登录
			Map<String, Object> paramMap = new HashMap<String, Object>();
			Member member = SessionContainer.getSessionMember(request);// 根据sessionId获取登录用户
			if(member!=null){
				String mName = member.getmId();
				paramMap.put("creater", mName);
			}
			paramMap.put("type", Constants.TYPE_C);  //3、我的足迹' 
			consultList = consultCommentService.queryLatelyTracks(paramMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return consultList;
	}
	
}
